package com.ruoyi.blindBoxManage.service.impl;

import com.ruoyi.blindBoxManage.domain.Class;
import com.ruoyi.blindBoxManage.mapper.BlindBoxMapper;
import com.ruoyi.blindBoxManage.mapper.BlindBoxProductMapper;
import com.ruoyi.blindBoxManage.mapper.ClassMapper;
import com.ruoyi.blindBoxManage.service.IClassService;
import com.ruoyi.common.utils.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@Service
public class ClassServiceImpl implements IClassService {
    @Autowired
    private ClassMapper classMapper;
    @Resource
    private BlindBoxMapper blindBoxMapper;
    @Resource
    private BlindBoxProductMapper blindBoxProductMapper;

    /**
     * 查询分类
     *
     * @param id 分类主键
     * @return 分类
     */
    @Override
    public Class selectClassById(Integer id) {
        return classMapper.selectClassById(id);
    }

    /**
     * 查询分类列表
     *
     * @param classs 分类
     * @return 分类
     */
    @Override
    public List<Class> selectClassList(Class classs) {
        return classMapper.selectClassList(classs);
    }

    /**
     * 新增分类
     *
     * @param classs 分类
     * @return 结果
     */
    @Override
    public int insertClass(Class classs) {
        classs.setCreateTime(DateUtils.getNowDate());
        return classMapper.insertClass(classs);
    }

    /**
     * 修改分类
     *
     * @param classs 分类
     * @return 结果
     */
    @Override
    public int updateClass(Class classs) {
        classs.setUpdateTime(DateUtils.getNowDate());
        return classMapper.updateClass(classs);
    }

    /**
     * 批量删除分类
     *
     * @param ids 需要删除的分类主键
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int deleteClassByIds(Integer[] ids) {
        for (Integer id : ids) {
            //查询盲盒是否绑定分类
            Integer count = blindBoxMapper.selectCountByClassId(id);
            if (count > 0) {
                throw new RuntimeException("id为:" + id + " 有盲盒绑定");
            }
            Integer count1 = blindBoxProductMapper.selectCountByClassId(id);
            if (count1 > 0) {
                throw new RuntimeException("id为:" + id + " 有商品绑定");
            }
        }
        return classMapper.deleteClassByIds(ids);
    }

    /**
     * 删除分类信息
     *
     * @param id 分类主键
     * @return 结果
     */
    @Override
    public int deleteClassById(Integer id) {
        return classMapper.deleteClassById(id);
    }
}
